package generator.main.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import generator.main.bean.Table;
import generator.main.bean.TableField;

/***
 * freemark 工具包
 * @author 郭军辉
 *
 * 2017年8月13日上午3:18:49
 */
public class ConfigurationUtils {
	
	/***
	 * 根据 模版路径创建模版 
	 * @param templatePath   	"G:/template"
	 * @return					 Configuration
	 * @throws IOException		 文件有几率找不到
	 */
	public static Configuration getConfiguration(String templatePath) throws IOException {
		// 创建一个合适的Configration对象
		Configuration configuration = new Configuration();
		configuration.setDirectoryForTemplateLoading(new File(templatePath));
		configuration.setObjectWrapper(new DefaultObjectWrapper());
		configuration.setDefaultEncoding("UTF-8"); // 这个一定要设置，不然在生成的页面中 会乱码
		return configuration;
	}
	
	
	
	/**
	 * 生成文件
	 * @param fieldMap  		字段集合
	 * 1：tableAllFiledList  	表中所有的字段
     * 2：tableFiledList  		不包含sts，创建人，创建人时间，修改人，修改人时间的集合
     * 3：insertTableFiledList  	插入字段的集合，不包含id，创建时间，修改时间，sts
     * 4：updateTableFiledList  	修改字段的集合，不包含：修改时间，sts
	 * @param table				表基本类型，（库名，表名，类介绍，路径，需要生成的类名称）
	 * @param configuration		freemark 模版
	 * @throws Exception 
	 */
	public static void writerFile(Map<String, List<TableField>> fieldMap, Table table ,Configuration configuration) throws Exception {
		/** 不包创建时间，修改时间，创建人，修改人，sts的集合*/
		List<TableField> tableFiledList =fieldMap.get("tableFiledList");
		/** 所有字段的集合*/
		List<TableField> tableAllFiledList =fieldMap.get("tableAllFiledList");
		/** 插入字段集合*/
		List<TableField> insertTableFiledList=fieldMap.get("insertTableFiledList");
		/**  修改字段集合*/
		List<TableField> updateTableFiledList=fieldMap.get("updateTableFiledList");
			Map<String, Object> paramMap = new HashMap<String, Object>();
			/** 类名称 */
			String className= table.getClassName();
			String entityName= table.getClassName()+"Entity";
			paramMap.put("entityName",entityName);
			paramMap.put("className",className);
			paramMap.put("remarks", table.getRemarks());
			paramMap.put("tableName", table.getTableName());
			paramMap.put("pageName", table.getPageName());
			paramMap.put("pagePath", table.getPath());
			/** 所有字段的集合*/
			paramMap.put("tableAllFiledList", tableAllFiledList);
			paramMap.put("updateTableFiledList", updateTableFiledList);
			/** 不包含创建时间，修改时间，sts*/
			paramMap.put("insertTableFiledList", insertTableFiledList);
			/** 不包含修改时间，创建时间，修改人，创建人，sts的集合 */
			paramMap.put("fieldList", tableFiledList);
			paramMap.put("basePackagePath",ConstantClassField.BASEPACKAGEPATH);
			paramMap.put("tablePath", table.getPath().replaceAll("_", "."));
//			System.out.println(ConstantClassField.BASEPACKAGEPATH+table.getPath().replaceAll("_", "."));
			System.out.println("================生成src开始======================");
			generatorSrc(table, configuration, paramMap, className, entityName);
			System.out.println("================生成src结束======================");
			System.out.println("================生成页面开始======================");
			generatorPages(table, configuration, paramMap, className, entityName);
			System.out.println("================生成页面结束======================");
	}


	/**
	 * 生成src文件
	 * @param table 				表基本类型，（库名，表名，类介绍，路径，需要生成的类名称）
	 * @param configuration			freemark模版
	 * @param paramMap				生成参数
	 * @param className				定义好格式的类名	
	 * @param entityName			定义好的实体映射名称
	 * @throws Exception
	 */
	private static void generatorPages(Table table, Configuration configuration, Map<String, Object> paramMap, String className, String entityName) throws Exception {
		/** 获取一个list模版。*/
		Template listTemplate = configuration.getTemplate("pages/list.jsp");
		/** 获取一个add模版。*/
		Template addTemplate = configuration.getTemplate("pages/add.jsp");
		/** 获取一个 Edit模版。*/
		Template editTemplate = configuration.getTemplate("pages/edit.jsp");
		/** 获取一个show模版。*/
		Template showTemplate = configuration.getTemplate("pages/show.jsp");
		/** 获取一个addVO模版。*/
		Template addVOTemplate = configuration.getTemplate("pages/vo/addVO.jsp");
		/** 获取一个editVO模版。*/
		Template editVOTemplate = configuration.getTemplate("pages/vo/editVO.jsp");
		/** 获取一个showVO模版。*/
		Template showVOTemplate = configuration.getTemplate("pages/vo/showVO.jsp");
		/** 获取一个conditionVO模版。*/
		Template conditionVOTemplate = configuration.getTemplate("pages/vo/conditionVO.jsp");
		/** 获取一个listComponent模版。*/
		Template listComponentTemplate = configuration.getTemplate("pages/page/listComponent.jsp");
	 
		/** vo路径*/
		String listPath	 =   table.getPath().replaceAll("_", "\\\\")+"\\"+table.getPageName()+"\\";
		/** page路径*/
		String pagePath = table.getPath().replaceAll("_", "\\\\")+"\\"+table.getPageName()+ "\\"+ "page" + "\\";
		/** vo路径*/
		String voPath	 =   table.getPath().replaceAll("_", "\\\\")+"\\"+table.getPageName()+"\\"  + "vo" + "\\";
		/** 创建controller路径*/
		FileUtils.createDir(ConstantClassField.PROJECTPAGESPATH+pagePath);
		/** 创建vo路径*/
		FileUtils.createDir(ConstantClassField.PROJECTPAGESPATH+voPath);
		
		/** 生成List.jsp文件*/
		Writer writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + listPath +"list.jsp"), "UTF-8");
		listTemplate.process(paramMap, writer);
		/** 生成add.jsp*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + listPath +"add.jsp"), "UTF-8");
		addTemplate.process(paramMap, writer);
		/** 生成edit.jsp*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + listPath +"edit.jsp"), "UTF-8");
		editTemplate.process(paramMap, writer);
		/** 生成show.jsp*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + listPath +"show.jsp"), "UTF-8");
		showTemplate.process(paramMap, writer);
		/** 生成addVO.jsp*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + voPath +"addVO.jsp"), "UTF-8");
		addVOTemplate.process(paramMap, writer);
		/** 生成editVO.jsp*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + voPath +"editVO.jsp"), "UTF-8");
		editVOTemplate.process(paramMap, writer);
		/** 生成showVO.jsp*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + voPath +"showVO.jsp"), "UTF-8");
		showVOTemplate.process(paramMap, writer);
		/** 生成conditionVO.jsp*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + voPath +"conditionVO.jsp"), "UTF-8");
		conditionVOTemplate.process(paramMap, writer);
		/** 生成listComponent.jsp*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTPAGESPATH + pagePath +"listComponent.jsp"), "UTF-8");
		listComponentTemplate.process(paramMap, writer);
 
	}
	
	
	/**
	 * 生成src文件
	 * @param table 				表基本类型，（库名，表名，类介绍，路径，需要生成的类名称）
	 * @param configuration			freemark模版
	 * @param paramMap				生成参数
	 * @param className				定义好格式的类名	
	 * @param entityName			定义好的实体映射名称
	 * @throws Exception
	 */
	private static void generatorSrc(Table table, Configuration configuration, Map<String, Object> paramMap, String className, String entityName) throws Exception {
		/** 获取一个 entity模版。*/
		Template entityTemplate = configuration.getTemplate("src/entity/entityTemplate.java");
		/** 获取一个 entityXML模版。*/
		Template entityXMLTemplate = configuration.getTemplate("src/entity/entityXMLTemplate.xml");
		/** 获取一个service模版。*/
		Template serviceTemplate = configuration.getTemplate("src/service/serviceTemplate.java");
		/** 获取一个serviceImpl模版。*/
		Template serviceImplTemplate = configuration.getTemplate("src/service/serviceImplTemplate.java");
		/** 获取一个daoImpl模版。*/
		Template daoImplTemplate = configuration.getTemplate("src/dao/daoImplTemplate.java");
		/** 获取一个dao模版。*/
		Template daoTemplate = configuration.getTemplate("src/dao/daoTemplate.java");
		/** 获取一个controller模版。*/
		Template controllerTemplate = configuration.getTemplate("src/controller/controllerTemplate.java");
		/** controller路径*/
		String controllerPath = table.getPath().replaceAll("_", "\\\\") + "\\"+ "controller" + "\\";
		/** service路径*/
		String servicePath	 =   table.getPath().replaceAll("_", "\\\\") + "\\"  + "service" + "\\";
		/** serviceImpl路径*/
		String serviceImplPath =  table.getPath().replaceAll("_", "\\\\") + "\\" + "service"+ "\\" + "impl" + "\\";
		/** dao路径*/
		String daoPath =   table.getPath().replaceAll("_", "\\\\") + "\\" + "dao" + "\\";
		/** daoImpl路径*/
		String daoImplPath =   table.getPath().replaceAll("_", "\\\\") + "\\" + "\\" + "dao" + "\\" + "\\" + "impl" + "\\" + "\\";
		/** entity路径*/
		String entityPath =   table.getPath().replaceAll("_", "\\\\") + "\\" + "entity" + "\\";
		/** 创建controller路径*/
		FileUtils.createDir(ConstantClassField.PROJECTSRCPATH+controllerPath);
		/** 创建service路径*/
		FileUtils.createDir(ConstantClassField.PROJECTSRCPATH+servicePath);
		/** 创建serviceImpl路径*/
		FileUtils.createDir(ConstantClassField.PROJECTSRCPATH+serviceImplPath);
		/** 创建dao路径*/
		FileUtils.createDir(ConstantClassField.PROJECTSRCPATH+daoPath);
		/** 创建daoImpl路径*/
		FileUtils.createDir(ConstantClassField.PROJECTSRCPATH+daoImplPath);
		/** 创建entity路径*/
		FileUtils.createDir(ConstantClassField.PROJECTSRCPATH+entityPath);
		/** 生成entity文件*/
		Writer writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTSRCPATH + entityPath + entityName+ ".java"), "UTF-8");
		entityTemplate.process(paramMap, writer);
		/** 生成xml*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTSRCPATH + entityPath + entityName+ ".xml"), "UTF-8");
		entityXMLTemplate.process(paramMap, writer);
		/** dao*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTSRCPATH + daoPath + "I"+className+"Dao" + ".java"), "UTF-8");
		daoTemplate.process(paramMap, writer);
		/** daoImpl*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTSRCPATH + daoImplPath +className+"DaoImpl" + ".java"), "UTF-8");
		daoImplTemplate.process(paramMap, writer);
		/** service*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTSRCPATH + servicePath + "I"+className+"Service" + ".java"), "UTF-8");
		serviceTemplate.process(paramMap, writer);
		/** serviceImpl*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTSRCPATH + serviceImplPath + className+"ServiceImpl" + ".java"), "UTF-8");
		serviceImplTemplate.process(paramMap, writer);
		/** controller*/
		writer = new OutputStreamWriter(new FileOutputStream(ConstantClassField.PROJECTSRCPATH + controllerPath + className+"Controller" + ".java"), "UTF-8");
		controllerTemplate.process(paramMap, writer);
	}
	
}
